package music

import (
	"context"
	"time"

	"go-weixin/app/music/musicapi/internal/svc"
	"go-weixin/app/music/musicapi/internal/types"
	"go-weixin/app/music/musicrpc/pb/proto/music"

	"github.com/zeromicro/go-zero/core/logx"
)

type MusicQueryTitleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewMusicQueryTitleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MusicQueryTitleLogic {
	return &MusicQueryTitleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *MusicQueryTitleLogic) MusicQueryTitle(req *types.MusicQueryTitleReq) (resp *types.MusicQueryTitleResp, err error) {
	ctx, canelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer canelFunc()
	res, err := l.svcCtx.MusicRpc.MusicQueryTitle(ctx, &music.MusicQueryTitleReq{
		Title: req.Title,
		Page:  req.Page,
	})
	if err != nil {
		return nil, err
	}
	var musicList []types.Music

	if len(res.MusicList) > 0 {
		for _, m := range res.MusicList {
			musicList = append(musicList, types.Music{
				Id:      m.Id,
				Title:   m.Title,
				Artist:  m.Artist,
				Path:    m.Path,
				Md5:     m.Md5,
				Year:    m.Year,
				Size:    m.Size,
				Pictrue: m.Pictrue,
			})
		}
	}
	resp = new(types.MusicQueryTitleResp)
	resp.MusicList = musicList
	return
}
